package com.token.iot.bratterycharging.boss.service;

import javax.jms.Message;
import javax.jms.ObjectMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.gson.Gson;
import com.outstanding.framework.base.TransactionLogger;
import com.outstanding.framework.base.mq.MQConsumer;
import com.outstanding.framework.base.mq.MessageType;
import com.outstanding.framework.core.PendingException;
import com.token.iot.bratterycharging.boss.web.ResCode;
import com.token.iot.bratterycharging.gateway.api.Constant;
import com.token.iot.bratterycharging.gateway.api.Device;
import com.token.iot.bratterycharging.gateway.api.FeeFinish;


/**
 * 设备上线监听器
 * @author chenzhixuan
 *
 */
@Component
public class DeviceOnLineConsumer {
	
	@Autowired
	private DeviceService deviceService;
	
	@MQConsumer(destination=Constant.MESSAGEHEAD+Constant.QueueDeviceON,messageModel=MessageType.PUBLISH_SUBSCRIBE)
	public void onMessageDeviceOff(Message message) throws PendingException{
		Gson gs = new Gson();
		try {
			String str = (String)((ObjectMessage)message).getObject();
			TransactionLogger.debug("DeviceOnLineConsumer on onMessageDeviceOff message"+str);
			Device d =gs.fromJson(str, Device.class);
			deviceService.onOnline(d);
		}catch(Exception e) {
			TransactionLogger.error("onMessageDeviceOff", e);
			throw new PendingException(ResCode.onMessgeDeviceOffError,e);
		}
	}
}
